package ca.uwindsor.cs.deepweb.estimation.method.poolbasedcoverage;

import java.util.ArrayList;

import org.apache.commons.math3.stat.descriptive.moment.Mean;
import org.apache.commons.math3.stat.descriptive.moment.StandardDeviation;

public class C3Stability {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// String index, String dict, int numberofdocs,
		// float coverage, int starting_df, String termlistfile,
		// String sampleindex, String selectedtermfile,
		// String termweightdffile, int numoftrials

		if (args.length != 10) {
			System.exit(-1);
		}
		int times = 50;

		ArrayList<Integer> t = new ArrayList<Integer>();
		t.add(10);
		t.add(50);
		t.add(100);
		t.add(500);
		t.add(1000);

		StandardDeviation sd = new StandardDeviation();
		Mean mean = new Mean();

		ArrayList<String[][]> list = new ArrayList<String[][]>(times);

		for (int i = 0; i < times; i++) {
			System.gc();
			int starting_df = Integer.parseInt(args[4]);
			C3 c3 = new C3(args[0], args[1], Integer.parseInt(args[2]), Float
					.parseFloat(args[3]), starting_df, args[5] + "_" + i,
					args[6] + "_" + i, args[7] + "_" + i, args[8] + "_" + i,
					Integer.parseInt(args[9]));
			try {
				c3.build();

				String[][] result = c3.estimate(t);
				System.out.println(result);
				list.add(result);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		String[][] result = list.get(0);
		String[][] rsd = new String[result.length][result[0].length];

		double[][] values = new double[result.length][list.size()];

		// compute RSD 'mean n' of 50 times
		for (int i = 0; i < list.size(); i++) {
			String[][] s = list.get(i);
			for (int j = 0; j < s.length; j++) {
				values[j][i] = Double.parseDouble(s[j][0]);
			}
		}
		for (int j = 0; j < result.length; j++) {
			rsd[j][0] = Double.toString(sd.evaluate(values[j])
					/ mean.evaluate(values[j]));
		}

		// compute RSD RB of 50 times
		for (int i = 0; i < list.size(); i++) {
			String[][] s = list.get(i);
			for (int j = 0; j < s.length; j++) {
				values[j][i] = Double.parseDouble(s[j][1]);
			}
		}
		for (int j = 0; j < result.length; j++) {
			rsd[j][1] = Double.toString(sd.evaluate(values[j])
					/ mean.evaluate(values[j]));
		}

		// compute RSD RSD of 50 times
		for (int i = 0; i < list.size(); i++) {
			String[][] s = list.get(i);
			for (int j = 0; j < s.length; j++) {
				values[j][i] = Double.parseDouble(s[j][2]);
			}
		}
		for (int j = 0; j < result.length; j++) {
			rsd[j][2] = Double.toString(sd.evaluate(values[j])
					/ mean.evaluate(values[j]));
		}
		
		//display
		for (int i = 0; i < rsd.length; i++) {
			for (int j = 0; j < rsd[i].length; j++) {
				System.out.print(rsd[i][j]);
				System.out.print("\t");
			}
			System.out.print("\n");
		}
	}

}
